package com.sirdc.modules.sys.aspect;

import com.sirdc.modules.sys.entity.SysLog;
import com.sirdc.modules.sys.service.SysLogService;
import jodd.log.Logger;
import jodd.log.LoggerFactory;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;


@Aspect
@Component
public class LogAspect {

    @Autowired
    private SysLogService logService;

    private  final Logger logger = LoggerFactory.getLogger(LogAspect.class);

    @Pointcut("execution(public * net.huashitong..*.*(..))")
    public void conctrllog(){}


    @Before("conctrllog()")
    public  void logafterCtrl(JoinPoint joinpoint)
    {
        SysLog log = new SysLog();
        logger.info("==================前置执行=====================>");
        System.out.println(" ==================前置执行=====================>");
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        System.out.println("请求来源： =》" + request.getRemoteAddr());
        System.out.println("请求URL：" + request.getRequestURL().toString());
        System.out.println("请求方式：" + request.getMethod());
        System.out.println("################CLASS_METHOD : " + joinpoint.getSignature().getDeclaringTypeName() + "." + joinpoint.getSignature().getName());
        System.out.println("响应方法：" + joinpoint.getSignature().getDeclaringTypeName() + "." + joinpoint.getSignature().getName());
        System.out.println("请求参数：" + Arrays.toString(joinpoint.getArgs()));

        log.setType("1");
        log.setRemoteAddr(request.getRemoteAddr());
        log.setUserAgent(request.getHeader("user-agent"));
        log.setRequestUri(request.getRequestURI());
        log.setMethod(request.getMethod());
        log.setParams(request.getQueryString());
        logService.save(log);
        System.out.println("==================前置执行完成==================>");
    }

}
